import time

import requests
import json

"""
    https://www.zhihu.com/api/v3/feed/topstory/recommend?session_token=ed68143c100c61a27a3933dd06a45514&desktop=true&page_number=2&limit=6&action=down&after_id=5&ad_interval=1
    https://www.zhihu.com/api/v3/feed/topstory/recommend?session_token=ed68143c100c61a27a3933dd06a45514&desktop=true&page_number=3&limit=6&action=down&after_id=11&ad_interval=0
"""

url_recommend = f"https://www.zhihu.com/api/v3/feed/topstory/recommend?session_token=ed68143c100c61a27a3933dd06a45514&desktop=true&page_number=%d&limit=6&action=down&after_id=5&ad_interval=1"


def main():
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'cookie': '_zap=0e329e9d-dccc-4787-bc70-9e8ab7c9b285; d_c0="AIBSdIezGRWPTkE6yQDvQlLjUOxkmlgI1LI=|1655282036"; __snaker__id=eUshQIXRESGPxtbH; _9755xjdesxxd_=32; gdxidpyhxdE=SZNUXQBiriM5c01eCqPor%2F7bqfgHf%5CDrHv3u4J12%2B4qniPRKmUoBr91J94raq%5C4gjAzgC9VqTT%2BinxltOVIe6dbOId%5C0rZkJ6BbTQ2ZTN9NOcCNhkzdEWpXaMaGmpaKZMO9EPx8dNEpyddxUAsVv99ZN%5CcnrRpeA56RWxRnbTkHXCckp%3A1655284549915; YD00517437729195%3AWM_NI=yHADWCcg1rzNjh3PGIgXTJqmBqRnZSm0bUURtIdWbRtRY40NCBfqL2ZaOaYwRHVpB3tYd0kFQRfeFRSlU7WeoMribCw%2FiIHiNJeh1grnoSCNJU859PIKPdfYJgTDNxHqS28%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda170e2e6eeaeb349a397bb89e7258dbc8aa7d15b929e9b86c54aabb8fbd2f35b93ecbeb1e22af0fea7c3b92abb988fa5d649a1b9ff93f946aab8fdb1c643a5889cabf46782af9e8fd959aff0aeb6d85ab897acb8e15ab88e97d5b346819eaa92cf3a9abf8182dc7d92989a91c139b887acd1ea4e938eaa86c55a91b49bd8d66fad93be8ae149b5f1a7aacb638e96ab89b46db2b2a1b4c2468a8bf8d6bc5c8b95a085e5538ab99ca5cd748caf9dd3bb37e2a3; YD00517437729195%3AWM_TID=N8e1hWB4PjFBFEAAQUfUEzxpg9VA4rfY; q_c1=5f7a0eabc7d44ab293304f1a7410cb99|1655283664000|1655283664000; _xsrf=fcb645e0-8aca-441f-a6e0-4c168e53e39a; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1667634092,1668041957,1668575016,1668753660; NOT_UNREGISTER_WAITING=1; SESSIONID=M2J4ftT74Hds4Blh7LKIYrngfEhcYyMN1Y5eT8cu93W; JOID=UV0UBk7PrIhPyI7YLskcW21FO8E5hJrGe6j8iB394ug4_MqyQheF-yrIjNgqI-m9HMLhbu5mYbc-8jkun7qGp0c=; osd=UFsWBkvOqopPzY_eLMkZWmtHO8Q4gpjGfqn6ih344-46_M-zRBWF_ivOjtgvIu-_HMfgaOxmZLY48DkrnryEp0I=; tst=r; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1668754164; z_c0=2|1:0|10:1668754164|4:z_c0|80:MS4xQWZRMEF3QUFBQUFtQUFBQVlBSlZUU3U5WVdTMUdYeGlyRDZQWTY2LUw4elBwSEtGWVRWdEZnPT0=|b654283eefb7fcad726c495c6d2ba872925a00b045ec4248a38b7c66cbefb00b; KLBRSID=dc02df4a8178e8c4dfd0a3c8cbd8c726|1668754302|1668753660',
    }
    for index in range(1,101):
        r = requests.get(
            url=url_recommend % (index,),
            headers=headers
        )
        " scanf() "
        text_dict = json.loads(r.text)
        text_json = json.dumps(
            text_dict["data"],
            ensure_ascii=False,
            indent=4
        )
        del text_dict

        with open(f"other_files/data_csv/{index}.json","w",encoding="utf-8") as w_file:
            w_file.write(text_json)
        print(f"index为{index}的已爬取完毕,休眠0.5s")

        time.sleep(0.5)



if __name__ == "__main__":
    main()
